#include "CacheManager.h"
#include <Windows.h>

CacheManager::CacheManager(size_t maxSize)
    : m_maxSize(maxSize)
    , m_currentSize(0)
    , m_hitCount(0)
    , m_missCount(0) {
}

bool CacheManager::Get(const std::wstring& path, std::vector<uint8_t>& data, size_t offset, size_t length) {
    std::lock_guard<std::mutex> lock(m_mutex);
    
    auto it = m_cache.find(path);
    if (it != m_cache.end()) {
        // 缓存命中
        m_hitCount++;
        
        CacheEntry& entry = it->second;
        entry.lastAccess = time(nullptr);
        
        // 检查请求范围是否有效
        if (offset + length <= entry.data.size()) {
            data.assign(entry.data.begin() + offset, entry.data.begin() + offset + length);
            UpdateLRU(path);
            return true;
        }
    }
    
    m_missCount++;
    return false;
}

bool CacheManager::Put(const std::wstring& path, const std::vector<uint8_t>& data, size_t offset) {
    std::lock_guard<std::mutex> lock(m_mutex);
    
    // 检查是否需要驱逐其他缓存项
    EvictIfNeeded(data.size());
    
    auto it = m_cache.find(path);
    if (it == m_cache.end()) {
        // 新建缓存项
        CacheEntry entry;
        entry.data = data;
        entry.size = data.size();
        entry.lastAccess = time(nullptr);
        entry.dirty = true;
        
        m_cache[path] = entry;
        m_currentSize += data.size();
    } else {
        // 更新现有缓存项
        CacheEntry& entry = it->second;
        
        // 如果需要，扩展缓存项大小
        if (offset + data.size() > entry.data.size()) {
            entry.data.resize(offset + data.size());
        }
        
        // 复制新数据
        std::copy(data.begin(), data.end(), entry.data.begin() + offset);
        entry.lastAccess = time(nullptr);
        entry.dirty = true;
    }
    
    UpdateLRU(path);
    return true;
}

void CacheManager::Flush() {
    std::lock_guard<std::mutex> lock(m_mutex);
    
    for (auto& pair : m_cache) {
        if (pair.second.dirty) {
            // 这里需要实现实际的磁盘写入逻辑
            // WriteToFile(pair.first, pair.second.data);
            pair.second.dirty = false;
        }
    }
}

void CacheManager::Remove(const std::wstring& path) {
    std::lock_guard<std::mutex> lock(m_mutex);
    
    auto it = m_cache.find(path);
    if (it != m_cache.end()) {
        m_currentSize -= it->second.size;
        m_cache.erase(it);
        
        // 从LRU列表中移除
        m_lru.remove(path);
    }
}

void CacheManager::EvictIfNeeded(size_t neededSize) {
    while (m_currentSize + neededSize > m_maxSize && !m_lru.empty()) {
        // 从最少使用的开始移除
        const std::wstring& path = m_lru.back();
        auto it = m_cache.find(path);
        if (it != m_cache.end()) {
            if (it->second.dirty) {
                // 如果是脏数据，需要先写回磁盘
                // WriteToFile(path, it->second.data);
            }
            m_currentSize -= it->second.size;
            m_cache.erase(it);
        }
        m_lru.pop_back();
    }
}

void CacheManager::UpdateLRU(const std::wstring& path) {
    // 将访问的项移到LRU列表前端
    m_lru.remove(path);
    m_lru.push_front(path);
}

void CacheManager::GetStats(size_t& hitCount, size_t& missCount) const {
    std::lock_guard<std::mutex> lock(m_mutex);
    hitCount = m_hitCount;
    missCount = m_missCount;
}
